﻿using System;
using System.Data.Common;
using System.IO;
using System.Text;
using System.Xml;
using System.Xml.XPath;

namespace Songhay.Data.GenericWeb.OutputDefinitions
{
    using Songhay.Data.GenericWeb.Procedures;

    /// <summary>
    /// Output procedures from the DBMS
    /// for <see cref="System.Xml.XPath.IXPathNavigable"/> types.
    /// </summary>
    public static partial class XPathNavigableOutput
    {
        /// <summary>
        /// Output new Document data for editing scenarios.
        /// </summary>
        /// <param name="commonConnection">The common connection.</param>
        /// <returns>Returns <see cref="System.Xml.XPath.XPathDocument"/>.</returns>
        public static XPathDocument NewDocument(DbConnection commonConnection)
        {
            string templates = List.Templates(commonConnection);
            string segments = List.Segments(commonConnection);

            XPathDocument d = null;
            using(MemoryStream stream = new MemoryStream())
            {
                using(XmlTextWriter writer = new XmlTextWriter(stream, Encoding.UTF8))
                {

                    writer.WriteStartDocument();
                    writer.WriteStartElement("ServerData");

                    writer.WriteStartElement("Document");

                    writer.WriteStartElement("DocumentId");
                    writer.WriteFullEndElement();
                    writer.WriteStartElement("Title");
                    writer.WriteFullEndElement();
                    writer.WriteStartElement("DocumentShortName");
                    writer.WriteFullEndElement();
                    writer.WriteStartElement("FileName");
                    writer.WriteFullEndElement();
                    writer.WriteStartElement("Path");
                    writer.WriteFullEndElement();

                    writer.WriteElementString("CreateDate", DateTime.Now.ToString(CommandHandler.GlobalDateFormat));
                    writer.WriteElementString("ModificationDate", DateTime.Now.ToString(CommandHandler.GlobalDateFormat));

                    writer.WriteStartElement("TemplateId");
                    writer.WriteFullEndElement();
                    writer.WriteStartElement("IsRoot");
                    writer.WriteFullEndElement();

                    writer.WriteElementString("IsActive", "true");

                    writer.WriteStartElement("SortOrdinal");
                    writer.WriteFullEndElement();
                    writer.WriteStartElement("ClientId");
                    writer.WriteFullEndElement();
                    writer.WriteStartElement("Tag");
                    writer.WriteFullEndElement();

                    writer.WriteFullEndElement();

                    writer.WriteRaw(templates);
                    writer.WriteRaw(segments);

                    writer.WriteFullEndElement();
                    writer.WriteEndDocument();

                    writer.Flush();

                    stream.Position = 0;
                    d = new XPathDocument(stream);
                }
            }

            return d;
        }
    }
}
